//
//  citipadViewController.m
//  citipad
//
//  Created by Chih Yuan Liu on 2/16/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "citipadViewController.h"
#import "CTPAlert.h"
#import "CTPBaseUrlFetcher.h"
#import "CTPWikiProxyFetcher.h"
#import "CTPFlickrFetcher.h"
#import "CTPFlickrFirstFetcher.h"
#import "modalImageView.h"
#import "modalMapView.h"
#import "citipadCityViewController.h"

@implementation citipadViewController

NSString *_cityname;
NSString *_spotname;


@synthesize scrollView;
@synthesize mapView;
@synthesize imageview;
@synthesize textView;
@synthesize cityNameLabel;
@synthesize spotNameLabel;
@synthesize backCityButton;
@synthesize titleImageView;

-(void)setCityName:(NSString*)name{
	_cityname = name;
}

-(void)setSpotName:(NSString*)name{
	_spotname = name;
}

/*
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
*/

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	//setup
	[scrollView setContentSize:CGSizeMake((2 * 1000), [scrollView bounds].size.height)];
	
	self.spotNameLabel.text = _spotname;
	
	self.cityNameLabel.text = _cityname;
	
	[self.backCityButton setTitle: _cityname forState: UIControlStateNormal];
	
	scrollView.backgroundColor = 
	[UIColor colorWithPatternImage:[UIImage imageNamed:@"s1.png"]]; 
	
	[imageview addGestureRecognizer:
	 [[[UITapGestureRecognizer alloc] initWithTarget:self
											  action:@selector(onImageClick)] autorelease]];
	
	[mapView addGestureRecognizer:
	 [[[UITapGestureRecognizer alloc] initWithTarget:self
											  action:@selector(onMapClick)] autorelease]];
	
	CTPFlickrTiledFetcher *fetcher = [CTPFlickrTiledFetcher alloc];
	fetcher.delegate = self;
	[fetcher getDataFromCtp:_spotname];//@"shanghai"];//"西門町"];  
	
	
	CTPFlickrFetcher * fetcher2 = [CTPFlickrFetcher alloc];
	fetcher2.delegate = self;
	 [fetcher2 getDataFromFlickr:_spotname];//@"taipei"];

	
	CTPGeocodeFetcher * geocoder = [CTPGeocodeFetcher alloc];
	geocoder.delegate = self;
	 [geocoder geocode:_spotname];//@"taipei"];
	
	
	CTPWikiProxyFetcher *fetcher3 =[CTPWikiProxyFetcher alloc];
	fetcher3.delegate = self;
	[fetcher3 getDataFromProxy:@"上海市"];
	
	CTPFlickrFirstFetcher *fetcher4 = [CTPFlickrFirstFetcher alloc];
	fetcher4.delegate = self;
	[fetcher4 getDataFromCtp:_spotname];
	//[[CTPAlert instance] alert:@"Jeff" withMessage:@""];
}



// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return (interfaceOrientation ==
			UIInterfaceOrientationLandscapeLeft);
}

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}


- (void)flickrTiledFetcherBegin:(CTPFlickrTiledFetcher*)sender{}
- (void)flickrTiledFetcherEnd:(CTPFlickrTiledFetcher*)sender{}
- (void)flickrTiledFetcherSucceeded:(CTPFlickrTiledFetcher*)sender data:(NSData*)data
{
	UIImage *image = [UIImage imageWithData:data];
	//UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
	//[scrollView addSubview:imageView];
	[self.imageview setImage:image];
}
- (void)flickrTiledFetcherFailed:(CTPFlickrTiledFetcher*)sender{}


- (void)flickrFetcherBegin:(CTPFlickrFetcher*)sender{}
- (void)flickrFetcherEnd:(CTPFlickrFetcher*)sender{}
- (void)flickrFetcherSucceeded:(CTPFlickrFetcher*)sender data:(NSArray*)data{
	NSLog(@"%i", data.count);
}
- (void)flickrFetcherFailed:(CTPFlickrFetcher*)sender{}

- (void)geocodeFetcherBegin:(CTPGeocodeFetcher*)sender{}
- (void)geocodeFetcherEnd:(CTPGeocodeFetcher*)sender{}
- (void)geocodeFetcherSucceeded:(CTPGeocodeFetcher*)sender lat:(double)lat lng:(double)lng {
	NSLog(@"%f,%f", lat, lng);
	MKCoordinateRegion newRegion;
    newRegion.center.latitude = lat;
    newRegion.center.longitude =lng;
    newRegion.span.latitudeDelta = 0.015;
    newRegion.span.longitudeDelta = 0.015;
	
    [self.mapView setRegion:newRegion animated:YES];
}
- (void)geocodeFetcherFailed:(CTPGeocodeFetcher*)sender{}

- (void)wikiProxyFetcherBegin:(CTPWikiProxyFetcher*)sender{}
- (void)wikiProxyFetcherEnd:(CTPWikiProxyFetcher*)sender{}
- (void)wikiProxyFetcherSucceeded:(CTPWikiProxyFetcher*)sender data:(NSArray*)data{
	NSString * value = (NSString*)[data objectAtIndex:0];
	NSString * decoded = [value stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
	[self.textView setText:decoded];
	NSLog(@"%@", decoded);
}
- (void)wikiProxyFetcherFailed:(CTPWikiProxyFetcher*)sender{
	[self.textView setText:@"Failed"];
}

- (void)flickrFirstFetcherBegin:(CTPFlickrFirstFetcher*)sender{}
- (void)flickrFirstFetcherEnd:(CTPFlickrFirstFetcher*)sender{}
- (void)flickrFirstFetcherSucceeded:(CTPFlickrFirstFetcher*)sender data:(NSData*)data{
	UIImage *image = [UIImage imageWithData:data];
	[self.titleImageView setImage:image];
	NSLog(@"recieved data");
}
- (void)flickrFirstFetcherFailed:(CTPFlickrFirstFetcher*)sender{}

modalImageView *miv;
modalMapView *mmv;

-(void) onImageClick{
	miv = [[modalImageView alloc] init];
	[miv setImage:imageview.image];
	miv.modalPresentationStyle = UIModalPresentationPageSheet;
	[self presentModalViewController:miv animated:YES];
}

-(void) onMapClick{
	mmv = [[modalMapView alloc]init];
	CLLocationCoordinate2D centre = [mapView centerCoordinate];
	[mmv setLocation:centre.latitude lng:centre.longitude];
	[mmv setAnnoTitle: spotNameLabel.text subtitle: _cityname];
	//mmv.modalPresentationStyle = UIModalPresentationPageSheet;
	[self presentModalViewController:mmv animated:YES];
}

-(IBAction) gobackCity{
	if([self.parentViewController isKindOfClass:[citipadCityViewController class]]){
		[self.parentViewController dismissModalViewControllerAnimated:YES];
	}else {
		[self.parentViewController.parentViewController dismissModalViewControllerAnimated:YES];
	}



}

@end
